package com.ruoyi.driver.service.impl;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;

import cn.hutool.core.io.file.FileNameUtil;
import com.github.tobato.fastdfs.domain.fdfs.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.driver.domain.TbDriverTurnover;
import com.ruoyi.driver.mapper.TbDriverTurnoverMapper;
import com.ruoyi.driver.ocrUnit.OcrUnit;
import com.ruoyi.driver.pojo.IdCardInfo;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.driver.mapper.TbDriverMapper;
import com.ruoyi.driver.domain.TbDriver;
import com.ruoyi.driver.service.ITbDriverService;
import org.springframework.web.multipart.MultipartFile;

/**
 * 司机管理Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-08-18
 */
@Service
public class TbDriverServiceImpl implements ITbDriverService 
{
    @Autowired
    private TbDriverMapper tbDriverMapper;

    @Autowired
    FastFileStorageClient fastFileStorageClient;
    @Autowired
    TbDriverTurnoverMapper tbDriverTurnoverMapper;
    /**
     * 查询司机管理
     * 
     * @param driverId 司机管理主键
     * @return 司机管理
     */
    @Override
    public TbDriver selectTbDriverByDriverId(Integer driverId)
    {
        return tbDriverMapper.selectTbDriverByDriverId(driverId);
    }

    /**
     * 查询司机管理列表
     * 
     * @param tbDriver 司机管理
     * @return 司机管理
     */
    @Override
    public List<TbDriver> selectTbDriverList(TbDriver tbDriver)
    {
        return tbDriverMapper.selectTbDriverList(tbDriver);
    }

    /**
     * 新增司机管理
     * 
     * @param tbDriver 司机管理
     * @return 结果
     */
    @Override
    public int insertTbDriver(TbDriver tbDriver)
    {
        return tbDriverMapper.insertTbDriver(tbDriver);
    }

    /**
     * 修改司机管理
     * 
     * @param tbDriver 司机管理
     * @return 结果
     */
    @Override
    public int updateTbDriver(TbDriver tbDriver)
    {
        return tbDriverMapper.updateTbDriver(tbDriver);
    }

    /**
     * 批量删除司机管理
     * 
     * @param driverIds 需要删除的司机管理主键
     * @return 结果
     */
    @Override
    public int deleteTbDriverByDriverIds(Integer[] driverIds)
    {
        return tbDriverMapper.deleteTbDriverByDriverIds(driverIds);
    }

    /**
     * 删除司机管理信息
     * 
     * @param driverId 司机管理主键
     * @return 结果
     */
    @Override
    public int deleteTbDriverByDriverId(Integer driverId)
    {
        return tbDriverMapper.deleteTbDriverByDriverId(driverId);
    }

    @Override
    public AjaxResult idCard(MultipartFile file) {
        try {
            String extName = FileNameUtil.extName(file.getOriginalFilename());
            StorePath storePath = fastFileStorageClient.uploadFile(file.getInputStream(), file.getSize(), extName, null);
          String url="http://192.168.80.192:8888/"+storePath.getFullPath();
            HashMap<Object, Object> map = new HashMap<>();
            String resultMsg = OcrUnit.ocrIdentification(1, file);
            IdCardInfo idCardInfo = OcrUnit.parseIdCardInfo(resultMsg);
            map.put("idCardInfo", idCardInfo);
            map.put("url", url);
            return AjaxResult.success(map);
        } catch (IOException e) {
           e.printStackTrace();
           return AjaxResult.error("OCR识别失败");
        }
    }

    @Override
    public AjaxResult updateStatus(TbDriver tbDriver) {
        //未启用 审核拒绝
        if(tbDriver.getWorkStatus()==1){
           tbDriver.setWorkStatus(1);
           tbDriverMapper.updateTbDriver(tbDriver);
            return AjaxResult.error("审核失败,请重新修改信息");
        }
       tbDriver.setWorkStatus(0);
       tbDriverMapper.updateTbDriver(tbDriver);
        return AjaxResult.success("审核通过,成为司机");
    }

    @Override
    public List<TbDriverTurnover> getTurnoverByDriverId(Integer driverId) {
        List<TbDriverTurnover> list = tbDriverTurnoverMapper.getTurnoverByDriverId(driverId);
        return list;
    }


}
